package ytdp.careagle.com.ytdp.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.umeng.analytics.dplus.UMADplus;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import ytdp.careagle.com.ytdp.R;
import ytdp.careagle.com.ytdp.activity.FinancialProgramActivity;
import ytdp.careagle.com.ytdp.adapter.base.CommonAdapter;
import ytdp.careagle.com.ytdp.adapter.base.MultiItemTypeAdapter;
import ytdp.careagle.com.ytdp.adapter.base.ViewHolder;
import ytdp.careagle.com.ytdp.app.manager.ImageLoadManager;
import ytdp.careagle.com.ytdp.entity.SellCarProgramEntity;
import ytdp.careagle.com.ytdp.entity.VehicleSeries;
import ytdp.careagle.com.ytdp.mvp.presenter.FragmentPresenter;
import ytdp.careagle.com.ytdp.mvp.view.IFragmentBannerView;
import ytdp.careagle.com.ytdp.myview.MyLinearManager;

public class FragmentBanner extends BaseFragment implements MultiItemTypeAdapter.OnItemClickListener, IFragmentBannerView {
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    private ArrayList<VehicleSeries> list;
    private CommonAdapter<VehicleSeries> adapter;
    private FragmentPresenter presenter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub  
        View view = inflater.inflate(R.layout.item_banner, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        Bundle arguments = getArguments();
        list = new ArrayList<>();
        list.add((VehicleSeries) arguments.getSerializable("item1"));
        list.add((VehicleSeries) arguments.getSerializable("item2"));
        adapter = new CommonAdapter<VehicleSeries>(getActivity(), R.layout.item_banner_carinfo, list) {
            @Override
            protected void convert(ViewHolder holder, VehicleSeries data, int position) {
                holder.setText(R.id.tv_title, data.getAdvertsing());
                holder.setText(R.id.tv_subTitle, data.getName());
                ImageView imageView = holder.getView(R.id.iv_car_img);
                ImageLoadManager.getImageLoadManager().load(data.getCoverImage(), imageView);
            }
        };
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
        presenter = new FragmentPresenter(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        UMADplus.track(getActivity(), "广告" + list.get(position).getName(), null);
        presenter.findVehicleByVehicleSeriesAndOnline(getActivity(), list.get(position).getId() + "");
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }

    @Override
    public void setCarData(List<SellCarProgramEntity> data) {
        Intent intent = new Intent(getActivity(), FinancialProgramActivity.class);
        intent.putExtra("data", (Serializable) data);
        startActivity(intent);
    }
}